Two methods having the same implementation are suspicious. It might be that something else was intended. Or the duplication is intentional, which
becomes a maintenance burden.
class Circle : public Shape {
private:
int radius;
public:
void setWidth(int size) {
radius = size / 2;
updateShape();
}
void setHeight(int size) { // Noncompliant: duplicates setWidth
radius = size / 2;
updateShape();
}
};
If the identical logic is intentional, the code should be refactored to avoid duplication. For example, by having both methods call the same method
or by having one implementation invoke the other.
class Circle : public Shape {
private:
int radius;
public:
void setWidth(int size) {
setDiameter(size);
}
void setHeight(int size) {
setDiameter(size);
}
private:
void setDiameter(int size) { // Implementation is shared
radius = size / 2;
updateShape();
}
};
This rule raises an issue on methods and not free functions.
Exceptions
Empty methods, methods with the same name (overload) and methods with only one statement are ignored.